<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
	</head>
	<body>	
<?
	// load dom-php lib and parse site data
	ini_set('error_reporting', E_ALL);
	ini_set('display_errors', 1);

	include('lib/simple_html_dom/simple_html_dom.php');
	$root = 'http://www.vmspark.ru';
	$url  = 'http://www.vmspark.ru/fiches';
	
	define('BR', '<br/>');

	// db_cfg
	$db_host	= 'localhost';
	$db_user	= 'root';
	$db_pass	= 'kscsq';
	$db_schema	= 'moto_parser';

	// db_connect
	if (($link = mysql_connect($db_host, $db_user, $db_pass)) && mysql_select_db($db_schema))
	{
		mysql_query('SET NAMES UTF8');
		echo 'db connection is ok'.BR;
	} 
	else
	{
		echo '! error: db connection is failed'.BR;
		exit;
	}

	// load DOM and parse it

	if ($dom = file_get_html($url))
	{
		echo 'i have a DOM master'.BR;
		// parses DOM
		$max_counter = 0;
		foreach ($dom->find('.bratd') as $element)
		{
			// testing
			if ($max_counter == 0)
			{
				$max_counter++;				
			}
			else
			{
				continue;
			}

			// catalog list
			$header = $element->find('b');
			if (!empty($header[0]))
			{			
				$utf8 = iconv('windows-1251', 'utf-8', $header[0]->innertext);
				// check and load sections
				
				$query = "SELECT * FROM section WHERE name = '".$utf8."'";
				$result = mysql_query($query);

				$parsed_section = false;

				if (mysql_num_rows($result) == 0)
				{
					echo '&nbsp;&nbsp;section <strong>'.$utf8.'</strong> add to db'.BR;
					$query = "INSERT INTO section SET name = '".$utf8."', parse = 0";
					mysql_query($query);
					$id = mysql_insert_id();
				}
				else
				{
					echo '&nbsp;&nbsp;section <strong>'.$utf8.'</strong> already exist in db'.BR;
					$section = mysql_fetch_assoc($result);					
					$parsed_section = $section['parse'] == 1 ? true : false;
					$id = $section['id'];
				}

				if (!$parsed_section)
				{
					echo '&nbsp;&nbsp;start parsing...'.BR;
					foreach ($element->find('a') as $catalog)
					{
						$sub_utf8 = iconv('windows-1251', 'utf-8', $catalog->innertext);
						echo '&nbsp;&nbsp;&nbsp;&nbsp;'.$sub_utf8.BR;

						// parse catalog in section												
						$sub_url = $root.$catalog->href;
						$sub_query = "SElECT * FROM catalog WHERE section = ".$id." AND href = '".$sub_url."'";
						$sub_result = mysql_query($sub_query);
						
						$parsed_catalog = false;
						if (!mysql_num_rows($sub_result))
						{
							$sub_query = "INSERT INTO catalog SET name = '".$sub_utf8."', href='".$sub_url."', section = ".$id.", parse = 0";						
							mysql_query($sub_query);
						}
						else
						{
							$db_catalog = mysql_fetch_assoc($sub_result);
							$parsed_catalog = $db_catalog['parse'] == 1 ? true : false;
						}

						if (!$parsed_catalog)
						{
							echo "&nbsp;&nbsp;&nbsp;&nbsp;need to parse redirecting".BR;
						}
						else
						{
							echo "&nbsp;&nbsp;&nbsp;&nbsp;already parsed finish".BR;
						}
					}
				} 
				else
				{
					echo '&nbsp;&nbsp;already parsed...'.BR;
				}				
			}			
		}			
	}
	else
	{
		echo '! error: url incorrect'.BR;
		exit;
	}	
	?>
</body>
</html>